<template>
  <a-modal
    title="新增装配工单"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <ass-table-form-item
          :dataSource="workOrderList"
          :proList="list.proList"
          :plaNum="form.getFieldValue('plaNum') ? form.getFieldValue('plaNum') : null"
          @add="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
          @del="
            ({ tableData }) => {
              workOrderList = tableData
            }
          "
        />
        <a-form-item label="备注信息" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-textarea :rows="3" placeholder="请输入备注信息" v-decorator="['remarks']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { assAdd } from '@/api/modular/main/ass/assManage'
  import assTableFormItem from '@/views/main/ass/assTableFormItem'
  import { proList } from '@/api/modular/main/pro/proManage'
  export default {
    components: { assTableFormItem },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        list: {
          // 产品
          proList: []
        },
        workOrderList: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    provide() {
      return {
        getProList: this.getProList
      }
    },
    methods: {
      // 初始化方法
      async add (record) {
        this.visible = true
        await this.getProList()
      },
      async getProList() {
        this.list.proList = []
        var promise = proList().then((res) => {
          this.list.proList = res.data
        })
        await promise
        return this.list.proList
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
              // 过滤空值
              if (this.workOrderList.length === 0) {
                this.confirmLoading = false
                return this.$message.error('产品不能为空')
              }
              values.workOrderList = this.workOrderList || null
            }
            assAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.workOrderList = []
      }
    }
  }
</script>
